#!/usr/bin/python
# coding=utf-8
import random, sys, logging, time, traceback
from datetime import date
from flask import Flask, request, json, jsonify, abort, make_response
import sys, datetime, json, logging, os
import pandas as pd
import numpy as np

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

profile = (sys.argv[1].split("=", 1)[-1]) if len(sys.argv) > 1 else 'dev'



@app.route('/api-model/get_info.sdo', methods=["GET", "POST"])
def test_index_1():
    response_dict = {'message': 'success---hi--prm-server'}
    return json.dumps(response_dict)


@app.route('/api-model/', methods=["GET", "POST"])
def test_index_0():
    response_dict = {'message': 'success---hi--model-server'}
    return json.dumps(response_dict)

# '/api-model/request_picture.sdo'这些可自定义，我最后拼接就是这样，10.70.8.138是部署的机器。http://10.70.8.138:7079/api-model/request_picture.sdo

@app.route('/api-model/request_picture.sdo', methods=["GET", "POST"])
def request_model():
    response_dict = dict()
    if request.method == 'POST':
        try:
            try:
                request_json_text = request.get_data()
                body_dict = json.loads(request_json_text.decode('utf-8'))
            except Exception as e:
                body_dict = request.form.to_dict()
                # NOTE below is the pseudo code of post data fetching
                # postdata = request.form['id']
                # file = request.files['file']
        except Exception as e:
            body_dict = None
            traceback.print_exc()
    else:
        body_dict = request.args.to_dict(flat=True)

    if body_dict is not None:
        try:
            code, msg = run_model_job(p_body_dict=body_dict)
        except Exception as e:
            code = -100
            msg = f'run_model_job_failed---with--{str(body_dict)}--{str(e)}'

    else:
        code = -200
        msg = 'invalid_http_request_parameters'

    response_dict['code'] = code
    response_dict['message'] = msg
    #传出的json
    return json.dumps(response_dict)
    # return bytes(json.dumps(response_dict), 'utf-8')
def run_model_job(p_body_dict):
    """

    """
    #程序开始
    row_data = p_body_dict
    #传入json的参数1，参数2
    coef1 = row_data['coef1']
    coef2 = row_data['coef2']
    code = 0
    msg = 'ok'
    return code,msg
if __name__ == '__main__':
    #port可自定义，我最后拼接就是这样，10.70.8.138是部署的机器。http://10.70.8.138:7079/api-model/request_picture.sdo

    app.run(host='0.0.0.0', port=7079)
